// pages/movie/movie.js
const {Data} = require("../../utils/data")
Page({
  data: {
    show:true,
    empty:true
  },
  onLoad: async function (options) {
    var module = await import("../../utils/http");
    var dataModule = await import("../../utils/data");
    var Top250Res = await module.MvHttp.getTop250();
    var comingRes = await module.MvHttp.getComingSoon();
    var inTheaterRes = await module.MvHttp.getInTheaters();
    Top250Res = dataModule.Data.handleHttpData(Top250Res.data.res);
    comingRes = dataModule.Data.handleHttpData(comingRes.data.res);
    inTheaterRes = dataModule.Data.handleinTheaterData(inTheaterRes.data.res);
    var all = [];
    all = this.allData(Top250Res,all);
    all = this.allData(comingRes,all);
    all = this.allData(inTheaterRes,all);
    var top250 = Top250Res.slice(0,3)
    var comingSoon = comingRes.slice(0,3)
    var inTheater = inTheaterRes.slice(0,3)
    this.setData({
      top250,
      comingSoon,
      inTheater,
      all
    })
  },
  allData(res,arr){
    res.forEach(item=>{
      arr.push(item);
    })
    return arr
  },
  bindKeyInput(event){
    var value = event.detail.value;
    var all = this.data.all;
    var searchMovie = [];
    if(value){
      this.setData({empty:false,show:false})
      all.forEach(item=>{
        item.title = item.title + "";
        if(item.title.includes(value)){
          searchMovie.push(item);
        }
      })
    }else{
      this.setData({empty:true})
    }
    this.setData({searchMovie})
  },
  onfocus(){
    this.setData({show:false})
  },
  onblur(){
    var empty = this.data.empty;
    if(empty){
      this.setData({show:true})
    }
  },
  
  toMore(event){
    var type = event.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/mvIndex/mvIndex?type=${type}`,
    })
  },

  toDetail(event){
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/mvDetail/mvDetail?id=${id}`,
    })
  },

  onTap(event){
    var id = event.currentTarget.dataset.aid;
    wx.navigateTo({
      url: `/pages/mvDetail/mvDetail?id=${id}`,
    })
  },
})